local console = ac.console 

local messager = class.panel:builder{
    x = 780,
    y = 760,
    w = 360,
    h = 60,
    text = {
        type = 'text',
        text = "",
        justH = "CENTER",
        justV = 'MIDDLE',
        color = 0xffffcc00,
        font_size = 14,
        shadow_color = 0xff000000,
        shadow_offset_x = 02,
        shadow_offset_y = -2,
        outline = true,
        w = 0,
    },
}

messager:set_level(100)

console.messager = messager

function messager:send(text, time)
    self.text:set_text(text)
    self:set_alpha(1.0)
    self.text:set_position((360 - self.text:get_width()) / 2, 0)
    self:set_position(780, 780)
    self:show()
    self.start = 780
    self.final = 700
    self.total = 0
    self.delay = time or 1.7
    self.switch = 1
end


function console:message(player, text, time)
    if (player and player:is_self()) or (not player) then 
        messager:send(text, time)
    end
end 

function console:error(player, text)
    if (player and player:is_self()) or (not player) then 
        messager:send(text, 1.7)
        ac.sound:error()
    end
end 

function ac.player.__index:console_message(message)
    console:message(self, message, 1.0)
end

function ac.player.__index:console_error(message)
    console:error(self, message)
end

ac.hardware:on "界面绘制" (function(_, FRAME)
    if messager.switch == 1 then 
        messager.total = messager.total + FRAME
        local progress = messager.total / messager.delay
        if progress >= 1 then 
            messager.switch = 0 
            messager:hide()
        else 
            messager:set_alpha(math.lerp(1, 0.8, progress))
            messager:set_position(messager.x, math.lerp(messager.start, messager.final, progress))
        end
    end 
end)

return messager